<template>
  <div class="info-main">
    <div class="info-left">
      <h3>任务名称： {{ datas.taskName }}</h3>
      <div>
        <p>源库地址：{{ form.ip + ":" + form.port }}</p>
      </div>
    </div>
    <div class="info-line" />
    <div class="info-right">
      <p>
        任务类型：
        <span @click="seeDatabaseInfo">{{ datas.taskType.split("-")[0] }}</span>
        <i class="el-icon-right"></i>
        <span>{{ datas.taskType.split("-")[1] }}</span>
      </p>
      <p>数据库端口：{{ form.port }}</p>
      <p>创建时间：{{ datas.createTime }}</p>
      <p>完成时间：{{ datas.endTime }}</p>
    </div>
    <div style="width: 20%;">
      <p v-for="item in successRateItem" :key="item.value">
        <span>{{ item.title }}：</span>
        <el-progress
          :text-inside="true"
          :stroke-width="24"
          :percentage="item.value * 100"
          status="success"
        ></el-progress>
      </p>
    </div>
    <!-- <div
      class="rate"
      :style="{
        color:
          successRate > 80
            ? 'rgb(145,204,117)'
            : successRate < 80 && successRate >= 60
            ? 'rgb(232,110,48)'
            : 'red',
      }"
    >
      {{ successRate }}
      <span>%</span>
    </div> -->

    <el-dialog
      title="数据源信息"
      width="800px"
      append-to-body
      :visible.sync="NextdataDialog"
    >
      <el-form ref="form" :model="form" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="实例名称" prop="dataSourceName">
              <el-input
                v-model="form.dataSourceName"
                placeholder="请输入名称"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驱动类" prop="driver">
              <el-input
                disabled
                v-model="form.driver"
                placeholder="请输入驱动类"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP" prop="ip">
              <el-input disabled v-model="form.ip" placeholder="请输入IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口" prop="port">
              <el-input disabled v-model="form.port" placeholder="请输入端口" />
            </el-form-item>
          </el-col>

          <el-col
            :span="12"
            v-for="item in dict.type.dabase_show"
            :key="item.value"
          >
            <el-form-item
              label="数据库"
              prop="databaseName"
              v-if="item.value == form.type"
            >
              <el-input
                disabled
                v-model="form.databaseName"
                placeholder="请输入数据库"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-for="item in dict.type.instancename_show"
            :key="item.value"
            :span="12"
          >
            <el-form-item
              v-if="item.value == form.type"
              label="实例名"
              prop="schemaName"
            >
              <el-input
                disabled
                v-model="form.schemaName"
                placeholder="请输入数据库实例名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户" prop="uname">
              <el-input
                disabled
                v-model="form.uname"
                placeholder="请输入用户"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="pwd">
              <el-input
                disabled
                type="password"
                v-model="form.pwd"
                placeholder="请输入密码"
                style="width: 270px"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <div style="margin-left: 3px; margin-bottom: 20px">
          高级配置
          <el-switch v-model="showConf" style="margin-left: 10px"> </el-switch>
        </div>
        <el-row v-if="showConf">
          <el-col :span="12">
            <el-form-item label="区分大小写" prop="caseSensitive">
              <el-radio-group disabled v-model="form.caseSensitive">
                <el-radio
                  v-for="dict in dict.type.database_case_sensitive"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自动提交" prop="autoCommit">
              <el-radio-group disabled v-model="form.autoCommit">
                <el-radio
                  v-for="dict in dict.type.database_auto_commit"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="连接超时" prop="connectTimeout">
              <el-input
                disabled
                v-model="form.connectTimeout"
                placeholder="请输入连接超时"
                onkeyup="value=(value.replace(/\D/g,'')==''?'':parseInt(value))"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会话超时" prop="socketTimeout">
              <el-input
                disabled
                v-model="form.socketTimeout"
                placeholder="请输入会话超时"
                onkeyup="value=(value.replace(/\D/g,'')==''?'':parseInt(value))"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码格式" prop="encoding">
              <el-input
                disabled
                v-model="form.encoding"
                placeholder="请输入编码格式"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Unicode" prop="useUnicode">
              <el-radio-group disabled v-model="form.useUnicode">
                <el-radio label="true">是</el-radio>
                <el-radio label="false">否</el-radio>
                <el-radio label="null">全不选</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group disabled v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.data_status"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-form-item label="URL" prop="url">
            <el-input disabled v-model="form.url" placeholder="请输入URL" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input
              disabled
              v-model="form.remark"
              type="textarea"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="testConnect">测试连接</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listDatasourceInfo,
  getDatasourceInfo,
  databaseCheck,
} from "@/api/console/datasourceInfo";

export default {
  name: "BaseInfo",
  dicts: [
    "database_case_sensitive",
    "data_status",
    "data_base_type",
    "database_auto_commit",
    "database_datatype",
    "database_datataststatus",
    "dabase_show",
    "instancename_show",
  ],
  props: ["datas"],
  data() {
    return {
      NextdataDialog: false,
      form: {},
      showConf: false,
      successRateItem: [],
      baseConf: {},
    };
  },
  created() {
    // console.log(JSON.parse(this.$props.datas.taskResult).data
    //   .compatiblePercent,'8888888');
    // return;
    let successRate = JSON.parse(this.$props.datas.taskResult).data
      .compatiblePercent;
    Object.keys(successRate).forEach((f) => {
      this.successRateItem.push({
        title: f,
        value: successRate[f].toFixed(2),
      });
    });
    this.baseConf = this.$props.datas;
  },
  mounted() {
    getDatasourceInfo({
      id: this.$props.datas.appraiseTasks.databaseId,
    }).then((response) => {
      this.form = response.data;
    });
  },
  methods: {
    seeDatabaseInfo() {
      this.NextdataDialog = true;
    },
    async testConnect() {
      const {
        type,
        ip,
        port,
        uname,
        pwd,
        databaseName,
        version,
        encoding,
        useUnicode,
        connectTimeout,
        socketTimeout,
        driver,
        url,
      } = this.form;
      const response = await databaseCheck({
        type,
        ip,
        port,
        uname,
        pwd,
        databaseName,
        version,
        encoding,
        useUnicode,
        connectTimeout,
        socketTimeout,
        driver,
        url,
      });
      if (response.data.code == 200) {
        this.$message({
          message: "连接成功",
          type: "success",
        });
        this.form.testStatus = 0;
      } else {
        this.form.testStatus = 1;
        this.$message.error("连接失败");
      }
      this.getList();
    },
    submitForm() {
      this.NextdataDialog = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.info-main {
  background: #fff;
  display: flex;
  align-items: center;
  border-radius: 2px;
  padding: 20px;
  margin: 15px 0;

  .info-left {
    width: 22%;

    p {
      color: #777;
    }
  }

  .info-line {
    width: 1px;
    height: 70px;
    border: 0.5px solid rgb(225, 228, 237);
    margin: 0 20px;
  }

  .info-right {
    width: 50%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    flex-wrap: wrap;
    p {
      width: 50%;
      color: #555;

      span:nth-child(1) {
        color: #4a4aff;
        cursor: pointer;
      }
    }
  }
}

.rate {
  font-size: 34px;
  font-style: italic;

  span {
    font-size: 20px;
  }
}
</style>
